package com.hbnu.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import java.util.Random;

public class PhoneCode {
    public static void sendCode(String phone,String code,int a){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","LTAI5t7fM7S1yHJRmkdVxUZV","85TWv6sJGHq6QVwAunN0Uq5LjvTTeq");
        IAcsClient client = new DefaultAcsClient(profile);// 2. 创建IAcsClient对象用于发送请求
        // 3. 创建请求对象
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        // 4. 设置请求参数
        request.putQueryParameter("RegionId","cn-hangzhou");
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("SignName","Liars酒馆");
        if (a == 1) {// 根据参数 `a` 判断使用哪个模板代码
            request.putQueryParameter("TemplateCode","SMS_475775100");
        } else{
            request.putQueryParameter("TemplateCode","SMS_475755090");
        }
        request.putQueryParameter("TemplateParam","{\"code\":\""+code+"\"}"); // 设置短信模板中占位符参数，如验证码
        try {
            CommonResponse response=client.getCommonResponse(request);// 5. 发送请求并接收响应
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    public static String getCode(){
        StringBuilder sb =new StringBuilder();
        for (int i = 0; sb.length()<6; i++) {
            int num=new Random().nextInt(10);
            sb.append(num);
        }
        return sb.toString();
    }
}
